﻿using System;

namespace MLSharp.Filters
{
	/// <summary>
	/// Removes attributes from a data set that are not 
	/// on a specified list of allowed attributes.
	/// </summary>
	public class AttributeIncluderFilter : AttributeFilterBase
	{
		#region Public Constructors

		/// <summary>
		/// Creates a new filter with no attributes included.
		/// </summary>
		public AttributeIncluderFilter() : base(new string[0])
		{
			
		}

		/// <summary>
		/// Filters everything except the specified attributes.
		/// </summary>
		/// <param name="attributeNames"></param>
		public AttributeIncluderFilter(params string[] attributeNames) : base(attributeNames)
		{
			if (AttributeNames.Length == 0)
			{
				throw (new ArgumentException("You must specify at least one attribute."));
			}
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Applies the filter to the specified data set.
		/// </summary>
		/// <param name="dataSet">The data set to transform.</param>
		public override void Transform(IDataSet dataSet)
		{
			//If there are no attributes to filter, just return.
			if (AttributeNames.Length == 0)
			{
				return;
			}

			for (int i = 0; i < dataSet.Attributes.Count; i++)
			{
				//If the current attribute is not on the list of attributes to keep,
				//remove it.
				if (Array.IndexOf(AttributeNames, dataSet.Attributes[i].Name) == -1)
				{
					dataSet.Attributes.RemoveAt(i);
					i--;
				}
			}
		}

		#endregion
	}
}
